package com.unicom.wostore.dsp.controller;

import com.unicom.wostore.dsp.utils.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Huangjie on 2017/3/7.
 */
@Controller
public class DispatcherAction {

    @RequestMapping(value = "getAds",method = {RequestMethod.POST},consumes = "application/json")
    @ResponseBody
    public JSONObject getAds(@RequestBody JSONObject jsonObj, HttpServletRequest httpServletRequest){
        AppcoachAction appcoachAction = new AppcoachAction();

        return appcoachAction.getAds(jsonObj,httpServletRequest);
    }





    public static void main(String[] args){
        JSONObject jsonObj = new JSONObject();

        JSONArray displayJsonArrObj = new JSONArray();
        JSONObject displayJsonObj = new JSONObject();
        displayJsonObj.put("media_id",1);
        displayJsonObj.put("size","320*50");
        //displayJsonObj.put("ads",1);
        displayJsonArrObj.add(0,displayJsonObj);
        jsonObj.put("display",displayJsonArrObj);

        JSONObject deviceJson = new JSONObject();
        deviceJson.put("imei","861602037165298");
        jsonObj.put("android",deviceJson);

        JSONObject osObj = new JSONObject();
        osObj.put("name","Android");
        osObj.put("version","7.0");
        jsonObj.put("os",osObj);

        jsonObj.put("imsi","460011875617090");
        jsonObj.put("network","wifi");

        JSONObject gpsJsonObj = new JSONObject();
        gpsJsonObj.put("lat",12.9921);
        gpsJsonObj.put("lng",29.222);
        jsonObj.put("gps",gpsJsonObj);



        jsonObj.put("ip","112.65.145.179");
        jsonObj.put("model","iphone7");
        jsonObj.put("pkgname","com.qq.wechat");
        jsonObj.put("sdk_ver","1.0");

        /*
        JSONObject gpsJsonObj = new JSONObject();
        gpsJsonObj.put("lat",123.9999);
        gpsJsonObj.put("lng",99.222);
        jsonObj.put("gps",gpsJsonObj);
        */


        System.out.println("Request="+jsonObj);

        AppcoachAction action = new AppcoachAction();
        action.logger.info("Main request data ="+jsonObj);


        //JSONObject response = HttpUtil.postJson("http://27.115.67.239:28082/dsp/getAds",jsonObj);

        JSONObject response = HttpUtil.postJson("https://127.0.0.1:8443/dsp/getAds",jsonObj,null);

        //JSONObject response = HttpUtil.postJson("http://127.0.0.1:8080/dsp/getAds",jsonObj);

        System.out.println("response111="+response);

    }
}


